{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ffef8d85-6a44-4e54-8b99-df99f63e3d6c",
   "metadata": {},
   "source": [
    "# Dummy EAS reconstruction\n",
    "\n",
    "This is a simple example on how GRANDlib classes can be used for data I/O in the case of shower parameters reconstruction. The traces used do not represent any real EAS traces, and the reconstruction used is completely meaningless, serving only ilustrational purpose.\n",
    "\n",
    "First, let's generate a ROOT file with random, dummy events. The old file will be cleared if exists. This uses an example script examples/io/DataStoringExample.py. Please be sure to restart kernel each time you try this script.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1550fe85-e21d-4423-8884-b8d90cb96af7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4 traces for event 0\n",
      "5 traces for event 1\n",
      "6 traces for event 2\n",
      "5 traces for event 3\n",
      "4 traces for event 4\n",
      "3 traces for event 5\n",
      "5 traces for event 6\n",
      "6 traces for event 7\n",
      "6 traces for event 8\n",
      "3 traces for event 9\n",
      "Wrote trun\n",
      "Wrote tadccounts\n",
      "Wrote trawvoltage\n",
      "Wrote tvoltage\n",
      "Wrote tefield\n",
      "Wrote tshower\n",
      "Finished writing file dummy_data.root\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os.path\n",
    "\n",
    "if os.path.isfile(\"dummy_data.root\"):\n",
    "    os.remove(\"dummy_data.root\")\n",
    "# Can't use %run - leaves stuff in memory that is needed empty later\n",
    "#%run ../io/DataStoringExample.py dummy_data.root\n",
    "os.system(\"../io/DataStoringExample.py dummy_data.root\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80d632ad-3633-4516-a4d5-b83dc1378f6d",
   "metadata": {},
   "source": [
    "Now we import the necessary python modules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4c65292a-a602-4ce1-8ba1-94e4a7b11c01",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "from grand.grandlib_classes.grandlib_classes import *\n",
    "import grand.grandlib_classes.grandlib_classes as g"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbc5931a-6774-48ab-b67d-6564e9def0f2",
   "metadata": {},
   "source": [
    "Let's create an Event, and provide the file name, run and event numbers (which we know to exist) for it"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b76571c5-7578-4a45-a86c-a612c996d552",
   "metadata": {},
   "outputs": [],
   "source": [
    "e = Event()\n",
    "e.file = \"dummy_data.root\"\n",
    "e.run_number = 0\n",
    "e.event_number = 0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02d7717a-9877-4092-a850-350e450240a2",
   "metadata": {},
   "source": [
    "The Event has to be told to initialise its contents from the provided file, with provided run and event numbers.\n",
    "We tell the Event that the tshower it reads from the file contains simulated (not reconstructed) values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "198b608d-648d-4af3-868b-a7732708ea2e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Run information loaded.\n",
      "Voltage information loaded.\n",
      "Efield information loaded.\n",
      "Shower information loaded.\n"
     ]
    }
   ],
   "source": [
    "e.fill_event_from_trees(simshower=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cda0986-7299-439b-b4a9-31d37bbf3d60",
   "metadata": {},
   "source": [
    "Just for checking - what are the contents of the beginning of the first trace X of the Efield?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "44fdbd33-5ffb-4509-af36-23f4f5d9c8b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.00218518  0.00141394 -0.00077124  0.0006427   0.00038562 -0.00141394\n",
      "  0.00154248 -0.00115686  0.00089978 -0.0006427 ]\n"
     ]
    }
   ],
   "source": [
    "print(e.efields[0].trace.x[:10])"
   ]
  },
  {
   "cell_type": "raw",
   "id": "4deeee77-0888-4d76-9f9c-37864d47a146",
   "metadata": {},
   "source": [
    "And what are the contents of the simulated shower?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2444eb86-de52-4beb-806a-d1c45857c801",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42240404.0 8.8195095 208.49489 -35.64038\n"
     ]
    }
   ],
   "source": [
    "print(e.simshower.energy_em, e.simshower.Xmax, e.simshower.azimuth, e.simshower.zenith)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e9476bf-8d31-43c7-a0c3-2cf6c0593931",
   "metadata": {},
   "source": [
    "Initialise the reconstruction shower in the Event:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8638026a-f8ec-4cd3-8553-f7f3390f8dcf",
   "metadata": {},
   "outputs": [],
   "source": [
    "e.shower = Shower()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b289d475-605b-4dbd-b1a8-a604c7037879",
   "metadata": {},
   "source": [
    "Storing the totally meaningless energy \"reconstruction\" in the Event:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9c236609-64ee-4f8d-8312-fae9b3bc5302",
   "metadata": {},
   "outputs": [],
   "source": [
    "e.shower.energy_em = 10**21*(np.max([np.max(np.sqrt(np.array(ef.trace.x)**2+np.array(ef.trace.y)**2+np.array(ef.trace.z)**2)) for ef in e.efields]))\n",
    "e.shower.energy_primary = 1.2*e.shower.energy_em"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2be2e6fa-21e1-4ad8-8358-d0775506250f",
   "metadata": {},
   "source": [
    "Storing the totally meaningless Xmax \"reconstruction\" in the Event:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "411cb037-05ae-41e5-a2d3-453b115db49b",
   "metadata": {},
   "outputs": [],
   "source": [
    "e.shower.Xmax = 1e6*(np.max([np.max(np.sqrt(np.array(ef.trace.x)**2+np.array(ef.trace.y)**2+np.array(ef.trace.z)**2)) for ef in e.efields]))\n",
    "\n",
    "e.shower.Xmaxpos.x = 1e5*np.sum([np.sum(ef.trace.x) for ef in e.efields])\n",
    "e.shower.Xmaxpos.y = 1e5*np.sum([np.sum(ef.trace.y) for ef in e.efields])\n",
    "e.shower.Xmaxpos.z = 1e5*np.sum([np.sum(ef.trace.z) for ef in e.efields])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d252c2c-bb00-4540-a7b7-275c93247868",
   "metadata": {},
   "source": [
    "Storing the totally meaningless angle reconstruction in the Event:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2a3fab13-5263-4feb-8b2e-3ff8289ebc8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "e.shower.zenith = np.arccos(e.shower.Xmaxpos.z/np.sqrt(e.shower.Xmaxpos.x**2+e.shower.Xmaxpos.y**2+e.shower.Xmaxpos.z**2))\n",
    "e.shower.azimuth = np.sign(e.shower.Xmaxpos.y)*np.arccos(e.shower.Xmaxpos.x/np.sqrt(e.shower.Xmaxpos.x**2+e.shower.Xmaxpos.y**2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fecfe52b-0120-434f-8c68-4ff1be6ff1a9",
   "metadata": {},
   "source": [
    "Write the reconstructed shower to another file:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a58ae506-eccb-448a-8460-8d2309dae6af",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "No valid tshower_reco TTree in the file dummy_data.root. Creating a new one.\n"
     ]
    }
   ],
   "source": [
    "if os.path.isfile(\"shower_reco.root\"):\n",
    "    os.remove(\"shower_reco.root\")\n",
    "\n",
    "#e.write_shower(\"shower_reco.root\")\n",
    "e.write_shower(\"dummy_data.root\", tree_name=\"tshower_reco\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57d26e56-1ac9-4315-983e-02bac6fc82a1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
